package beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import manager.WsdlManager;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.predic8.schema.Attribute;
import com.predic8.schema.ComplexType;
import com.predic8.schema.Element;
import com.predic8.schema.Schema;
import com.predic8.schema.SimpleType;
import com.predic8.schema.TypeDefinition;
import com.predic8.wsdl.Definitions;
import com.predic8.wsdl.Message;
import com.predic8.wsdl.Operation;
import com.predic8.wsdl.Part;
import com.predic8.wsdl.PortType;
import com.predic8.wsdl.WSDLParser;

@ManagedBean
@SessionScoped
public class WsdlParserBean {

	Definitions defs;

	List<SimpleType> simpleTypes;
	List<ComplexType> complexTypes;
	String operation;
	
	public WsdlParserBean() {
		// TODO Auto-generated constructor stub
		simpleTypes = new ArrayList<SimpleType>();
		operation = "controlCustomerKpsApsInformation";
		WsdlManager.initialize(defs);
	}
	
	public void listKeys() throws Exception {
		
		PortType p = defs.getPortType("ESBGenericPort");
		Operation o = p.getOperation(operation);
		List<Part> parts = o.getInput().getMessage().getParts();
		for (Part part : parts) {
			String uri = part.getType().getNamespaceURI();
			Schema sh = defs.getSchema(uri);
			if(sh == null)
				continue;
			if(part.getType() == null)
				continue;
			TypeDefinition type = sh.getType(part.getType().getLocalPart());
			if(type instanceof ComplexType){
				complexTypes.add((ComplexType)type);
				System.out.println("----complex----"); 
				ComplexType cType = (ComplexType) type;
				System.out.println(cType.getName()); 
				for (Element elem : cType.getSequence().getElements()) {
					System.out.println("	" + elem.getName()); 
				}
				System.out.println("----"); 
			}
			else if(type instanceof SimpleType){
				simpleTypes.add((SimpleType)type);
				System.out.println("----simple----"); 
				SimpleType cType = (SimpleType) type;
				System.out.println(cType.getName()); 
				System.out.println("----");
			}
			else {
				System.out.println("----unknown----");
				System.out.println("Type: " + part.getType());
			}
		}
		System.out.println("----Done----");
//		Element e = defs.getElement("GetOVDPAccountsInputType");

		 
//		Element element = defs.getElement(parts.get(0).getType().getLocalPart()); 
//		TypeDefinition td = element.getEmbeddedType(); 
//		ComplexType cp = (ComplexType) td; 
//		System.out.println("Complex Type: " + cp); 
//		model = cp.getModel(); 
		
//		Object ob = defs.getTypeString(parts.get(0).getType().getLocalPart());
//		for (PortType pt : defs.getPortTypes()) {
//			pt.getName()
//			System.out.println(pt.getName());
//			for (Operation op : pt.getOperations()) {
//				System.out.println(" -" + op.getName());
//				for (Part p : op.getInput().getMessage().getParts()) {
//					System.out.println(" 		-" + p.getName() + ": " + p.);
//				}
//				System.out.println("-------------------------------------------------------------------------------------");
//			}
//		}
	}

//	public static void getOperation() throws Exception {
//		WSDLParser parser = new WSDLParser();
//		Definitions defs = parser.parse(WSDL_URL);
//		
//		Operation operation = defs.getOperation("getOVDPAccounts", "");
//	}

	public List<SimpleType> getSimpleTypes() {
		return simpleTypes;
	}

	public void setSimpleTypes(List<SimpleType> simpleTypes) {
		this.simpleTypes = simpleTypes;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getOperation() {
		return operation;
	}

	public List<ComplexType> getComplexTypes() {
		return complexTypes;
	}

	public void setComplexTypes(List<ComplexType> complexTypes) {
		this.complexTypes = complexTypes;
	}
	
	
}
